//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：DrawDeployAssemblyBase
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
#if(UNITY_EDITOR)
using HybridCLR.Editor.Settings;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine;
namespace HDMCode.Hotfix
{
    public class DrawDeployAssemblyBase
    {
        /// <summary>
        /// 复制文件到指定文件夹
        /// </summary>
        public static void GetFiles()
        {
#if UNITY_ANDROID
            string matePath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.strippedAOTDllOutputRootDir}/Android";
            string hotPath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.hotUpdateDllCompileOutputRootDir}/Android";
#elif UNITY_IOS
            string matePath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.strippedAOTDllOutputRootDir}/iOS";
            string hotPath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.hotUpdateDllCompileOutputRootDir}/iOS";
#elif UNITY_WEBGL
            string matePath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.strippedAOTDllOutputRootDir}/WebGL";
            string hotPath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.hotUpdateDllCompileOutputRootDir}/WebGL";
#else
            string matePath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.strippedAOTDllOutputRootDir}/StandaloneWindows64";
            string hotPath = $"{System.Environment.CurrentDirectory}/{HybridCLRSettings.Instance.hotUpdateDllCompileOutputRootDir}/StandaloneWindows64";
#endif
            if (Directory.Exists(matePath) && Directory.Exists(hotPath))
            {
                string outMatePath = $"{DrawMeun.drawUISetting.ScriptOutPutPath}/MateDLL";
                string outHotPath = $"{DrawMeun.drawUISetting.ScriptOutPutPath}/HotDLL";
                if (Directory.Exists(outMatePath)) Directory.Delete(outMatePath, true);
                if (Directory.Exists(outHotPath)) Directory.Delete(outHotPath, true);
                Directory.CreateDirectory(outHotPath);
                Directory.CreateDirectory(outMatePath);

                DirectoryInfo mateDir = new DirectoryInfo(matePath);
                DirectoryInfo hotDid = new DirectoryInfo(hotPath);

                foreach (var mateFils in mateDir.GetFiles())
                {
                    File.Copy(mateFils.FullName, $"{outMatePath}/{mateFils.Name}.bytes");
                }
                foreach (var hotDllName in HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions)
                {
                    foreach (var hotfile in hotDid.GetFiles())
                    {
                        if ($"{hotDllName.name}.dll" == hotfile.Name)
                        {
                            File.Copy(hotfile.FullName, $"{outHotPath}/{hotfile.Name}.bytes");
                        }
                    }
                }
                AssetDatabase.Refresh();
            }
        }

        /// <summary>
        /// 判断文件夹是否正确
        /// </summary>
        public static void DirectoryCheck()
        {
            if (!Directory.Exists(DrawMeun.drawUISetting.AssetsOutPutPath))
            {
                Debug.LogError("输出文件夹未设置，请前往海盗猫框架设置路径！");
            }
            else
            {
                Directory.Delete(DrawMeun.drawUISetting.AssetsOutPutPath, true);
                Directory.CreateDirectory(DrawMeun.drawUISetting.AssetsOutPutPath);
            }

            if (!Directory.Exists(Application.streamingAssetsPath))
            {
                Debug.LogError($"StreamingAssets 路径文件夹不存在！");
            }
            else
            {
                if (Directory.Exists(Application.streamingAssetsPath + "/yoo"))
                {
                    AssetDatabase.DeleteAsset(Application.streamingAssetsPath + "/yoo");
                }
                Directory.CreateDirectory(Application.streamingAssetsPath + "/yoo");
            }
        }


        /// <summary>
        /// 打开EXE工具
        /// </summary>
        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        public static extern int ShellExecute(IntPtr hwnd, string lpszOp, string lpszFile, string lpszParams, string lpszDir, int FsShowCmd);
        public static void OpenEXETool(string Path, string IDEPath)
        {
            if (EditorApplication.isPlaying)
                EditorApplication.isPlaying = false;

            if (!string.IsNullOrEmpty(IDEPath))
                ShellExecute(IntPtr.Zero, "open", $"" + IDEPath, "", "", 1);
            else
                ShellExecute(IntPtr.Zero, "open", "notepad", "", "", 1);
        }
    }
}
#endif
